package acwing._1_1AlgorithmBasic;

/**
 * @Project : ProgrammingExercises
 * @Package : ACWing.Learning.AlgorithmTemplate
 * @File : _2_2StackQueue.java
 * @Author : WangRuoyu
 * @Date 2023/3/11 16:33
 */

public class _2_2StackQueue {
    public static void monotonyStack(int q[]) {
        int stk[] = new int[100010];
        int index = 0;

        stk[index++] = -1;
        for (int j : q) {
            if (j > stk[index - 1]) {
                System.out.println(stk[index - 1]);
                stk[index++] = j;
            } else {
                while (j <= stk[index - 1]) {
                    index--;
                }
                System.out.println(stk[index - 1]);
                stk[index++] = j;
            }
        }
    }

    public static void monotonyQueueMin(int q[], int k) {
        int[] queue = new int[100010];
        int head = 0;
        int tail = 0;

        for (int i = 0; i < q.length; i++) {
            if (head == tail) {
                queue[head++] = i;
            } else {
                while ((head > tail) && (q[i] < q[queue[head - 1]])) {
                    head--;
                }
                queue[head++] = i;
            }
            if ((i - k + 1) > queue[tail]) {
                tail++;
            }

            if (i >= k - 1) {
                System.out.print(q[queue[tail]] + " ");
            }
        }
    }

    public static void monotonyQueueMax(int q[], int k) {
        int[] queue = new int[100010];
        int head = 0;
        int tail = 0;

        for (int i = 0; i < q.length; i++) {
            if (head == tail) {
                queue[head++] = i;
            } else {
                while ((head > tail) && (q[i] > q[queue[head - 1]])) {
                    head--;
                }
                queue[head++] = i;
            }
            if ((i - k + 1) > queue[tail]) {
                tail++;
            }
            if (i >= k - 1) {
                System.out.print(q[queue[tail]] + " ");
            }
        }
    }
}

class stack {
    int[] stk = new int[100010];
    private int index;

    public stack() {
        index = 0;
    }

    boolean isempty() {
        return index == 0 ? true : false;
    }

    public int pop(int x) {
        stk[index] = x;
        index++;
        return 1;
    }

    public int push(int x) {
        if (isempty()) {
            return -1;
        }
        return stk[index--];
    }
}

class queue {
    int[] q = new int[100010];
    private int head;
    private int tail;

    public queue() {
        head = 0;
        tail = 0;
    }

    boolean isempty() {
        return head == tail ? true : false;
    }

    public int pop(int x) {
        q[head] = x;
        head++;
        return 1;
    }

    public int push(int x) {
        if (isempty()) {
            return -1;
        }
        return q[tail++];
    }
}



